package com.tbit.uqbike.push.service.impl;

import com.tbit.uqbike.push.constant.ControlTypeConstant;
import com.tbit.uqbike.push.constant.MachineConstant;
import com.tbit.uqbike.push.constant.RedisConstant;
import com.tbit.uqbike.push.constant.SystemData;
import com.tbit.uqbike.push.service.RedisService;
import com.tbit.uqbike.push.service.TerControlService;
import com.tbit.uqbike.push.service.ThreadService;
import com.tbit.uqbike.push.util.LogUtil;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 17:11 2022/12/21
 */
@Service
public class ForBiddenServiceImpl implements ThreadService {

    private RedisService redisService;
    private TerControlService terControlService;
    private String machineNO;

    public RedisService getRedisService() {
        return redisService;
    }

    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    public TerControlService getTerControlService() {
        return terControlService;
    }

    public void setTerControlService(TerControlService terControlService) {
        this.terControlService = terControlService;
    }

    public String getMachineNO() {
        return machineNO;
    }

    public void setMachineNO(String machineNO) {
        this.machineNO = machineNO;
    }

    @Override
    public void outsidePush() {
    }

    @Override
    public void insidePush() {

    }

    @Override
    public void warn() {
        String tmp = redisService.get(RedisConstant.REDIS_MACHINE_NEAR_FORBIDDEN_WARN + machineNO);
        if (tmp == null) {
            tmp = "0";
        }
        if (Integer.valueOf(tmp) < 3) {
            LogUtil.info("发送即将出禁行区车体语音预警[" + machineNO + "]");
            String sNo1 = UUID.randomUUID().toString();
            terControlService.sendControl(sNo1, machineNO, ControlTypeConstant.CONTROL_TYPE_VOICE, ControlTypeConstant.TER_VOICE_20, "", "即将出禁行区语音预警播报");
            redisService.add(RedisConstant.REDIS_MACHINE_NEAR_FORBIDDEN_WARN + machineNO,
                    String.valueOf(Integer.valueOf(tmp) + 1), 300);
        }
    }

    @Override
    public void isOut() {

    }

    @Override
    public void outForbidden() {
        String outPower = redisService.get(RedisConstant.REDIS_MACHINE_OUTFORBIDDEN_PUSH + machineNO);
        if (outPower == null) {
            outPower = "0";
        }
        Integer tmp = Integer.valueOf(outPower);
        if (tmp < 3) {
            LogUtil.info("发送禁行区车体语音通知[" + machineNO + "]");
            String sNo1 = UUID.randomUUID().toString();
            terControlService.sendControl(sNo1, machineNO, ControlTypeConstant.CONTROL_TYPE_VOICE, ControlTypeConstant.TER_VOICE_43, "", "禁行区语音播报");
            String serialNo = UUID.randomUUID().toString();
            LogUtil.info("machineNO=" + machineNO + "禁行区控制断电" + "serialNo=" + serialNo);
            Integer machineType = SystemData.machineNOMachineTypeMap.get(machineNO);
            if(MachineConstant.MACHINE_TYPE_ELECT_BIKE.equals(machineType)){
                terControlService.sendControl(serialNo, machineNO, ControlTypeConstant.CONTROL_TYPE_CONTROL, ControlTypeConstant.CONTROL_TYPE_UNSTART, "", "禁行区断电");
            }
            /**记录已发送断电指令*/
            redisService.add(RedisConstant.REDIS_MACHINE_OUTFORBIDDEN_PUSH + machineNO, String.valueOf(tmp + 1), 30 * 60);
        }
    }

    @Override
    public void insideForbidden() {
        String outPower = redisService.get(RedisConstant.REDIS_MACHINE_OUTFORBIDDEN_PUSH + machineNO);
        if (outPower == null) {
            outPower = "0";
        }
        Integer tmp = Integer.valueOf(outPower);
        if (tmp > 0) {
            /**控制上电*/
            String serialNo = UUID.randomUUID().toString();
            LogUtil.info("machineNO=" + machineNO + ",禁行区控制上电," + "serialNo=" + serialNo);
            //下发指令恢复上电
            terControlService.sendControl(serialNo, machineNO, ControlTypeConstant.CONTROL_TYPE_CONTROL, ControlTypeConstant.CONTROL_TYPE_UNLOCK, "", "出禁行区上电");
            /**清除断电缓存*/
            redisService.delet(RedisConstant.REDIS_MACHINE_OUTFORBIDDEN_PUSH + machineNO);

        }
    }
}
